package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.Appointment;
import com.czh.springboot04.service.AppointmentService;
import com.czh.springboot04.service.serviceimpl.AppointmentServiceImpl;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.List;

@RestController
@RequestMapping("appointment")
@CrossOrigin
public class AppointmentController {

    @Autowired
    DataSource dataSource;
    @Autowired
    AppointmentService appointmentService = new AppointmentServiceImpl();

    @PostMapping("/insertAppointment")
    public R insertAppointment(@RequestBody Appointment appointment){
        int i = appointmentService.insertAppointment(appointment);
        if (i!=0){
            return R.ok("预约成功！");
        }else {
            return R.fail("预约失败！");
        }
    }
    @PutMapping("/updateAppointmentById")
    public R updateAppointmentById(@RequestBody Appointment appointment){
        int i = appointmentService.updateAppointmentById(appointment);
        if (i!=0){
            return R.ok("修改成功！");
        }else {
            return R.fail("修改失败！");
        }
    }
    @GetMapping("/selectAppointmentById")
    public R selectAppointmentById(Integer id){
        Appointment appointment = appointmentService.selectAppointmentById(id);
        if (appointment!=null){
            return R.ok(appointment);
        }else {
            return R.fail("查询失败！");
        }
    }

    @GetMapping("/selectAppointmentAll")
    public R selectAppointmentAll(Integer consultId,
                                  Integer userId,
                                  Integer status,
                                  @RequestParam(required = false,defaultValue = "1") Integer pageNum,
                                  @RequestParam(required = false,defaultValue = "10") Integer pageSize){
        PageInfo<Appointment> appointmentPageInfo = appointmentService.selectAppointmentAll(consultId, userId, status, pageNum, pageSize);
        if (appointmentPageInfo!=null){
            return R.ok(appointmentPageInfo);
        }else {
            return R.fail("查询失败！");
        }
    }

    @DeleteMapping("/deleteAppointmentById/{id}")
    public R deleteAppointmentById(@PathVariable("id") Integer id){
        int i = appointmentService.deleteAppointmentById(id);
        if (i!=0){
            return R.ok("删除成功！");
        }else {
            return R.fail("删除失败！");
        }
    }

    @DeleteMapping("/deleteAppointmentByList")
    public R deleteAppointmentByList(@RequestBody List<Integer> idList){
        int i = appointmentService.deleteAppointmentByList(idList);
        if (i!=0){
            return R.ok("批量删除成功！");
        } else {
            return R.fail("批量删除失败！");
        }
    }
}
